import { Service } from "egg";
const labels = require('./label.json');
import * as blue from 'bluebird';

export default class CallService extends Service {
  async importCallLabel(): Promise<any> {
    
    const {app, ctx} = this;

    const accountId = ctx.request.query.accountId;
    ctx.logger.info('==>', accountId);
    if (!accountId) {
      return ctx.sendError('no account');
    }
    const findDoc = {accountId, type:"multi"};
    const res = await app.commonDB.db('cc').collection('manager_call_label')
      .updateMany(findDoc, {$set: {accountId: `${accountId}_bak`}});
    ctx.logger.info('res==>', res.result);

    await blue.mapSeries(labels, async(e: any, index, len) => {
      ctx.logger.info('==>', index, len, e.name);
      e.accountId = accountId;
      e.biUpdateTime = app.m7Utils.DateTime.getCurrentDateTime();
      const id =e._id;
      await app.commonDB.db('cc').collection('manager_call_label')
      .updateOne({_id: id}, {$set: e}, {upsert: true} );
    });
    ctx.logger.info('over===!');

  }
}